<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     * GET|HEAD|category| category.index
     * @return \Illuminate\Http\Response
     * 全部分类列表
     */
    public function index()
    {
        $data = (new Category)->tree();
        return view('Admin.category.index') -> with('data',$data);
    }
    /**
     * Show the form for creating a new resource.
     * | GET|HEAD| category/create|category.create
     * @return \Illuminate\Http\Response
     * 添加分类
     */
    public function create()
    {
        $cate = Category::where('cate_pid',0)->get();
        return view('Admin.category.add',compact('cate'));
    }

    /**
     * Store a newly created resource in storage.
     * |POST|category|category.store
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 添加分类提交
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');
        $rules =[
            'cate_name'=>'required',
        ];
        $message =[
            'cate_name.required' => '分类名称不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes())
        {
            $res = Category::create($input);
            if($res)
            {
                return redirect('Admin/category')->with('info','分类添加成功');
            }else
            {
                return back()->with('info','数据添加失败，请稍后再试');
            }
        }else
        {
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     * GET|HEAD| category/{category}| category.show
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 显示单个分类信息
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     * GET|HEAD| category/{category}/edit | category.edit
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 编辑分类
     */
    public function edit($id)
    {
        $data = Category::find($id);
        $cate = Category::where('cate_pid',0)->get();
        return view('Admin.category.edit',compact('data','cate'));
    }

    /**
     * Update the specified resource in storage.
     * PUT|PATCH|category/{category}| category.update
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 更新分类信息
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token','_method');
        $res = Category::where('cate_id',$id)->update($input);
        if($res)
        {
            return redirect('Admin/category')->with('info','分类更新成功');
        }else
        {
            return back()->with('info','分类更新失败，请稍候再试');
        }
    }

    /**
     * Remove the specified resource from storage.
     * | DELETE| category/{category}| category.destroy
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * 删除分类
     */
    public function destroy($id)
    {
        $res = Category::where('cate_id',$id)->delete();
        Category::where('cate_pid',$id)->update(['cate_pid'=>0]);
        if($res)
        {
           $data = [
               'status'=>0,
               'msg'=>'分类删除成功',
           ];
        }else
        {
            $data = [
                'status'=>1,
                'msg'=>'分类删除失败，请稍候再试',
            ];
        }
        return $data;
    }

    public function changeorder()
    {
        $input = Input::all();
        $cate = Category::find($input['cate_id']);
        $cate -> cate_order = $input['cate_order'];
        $res = $cate->update();
        if($res)
        {
            $date = [
                'status' => 0,
                'msg' => '分类更新成功',
            ];
        }else
        {
            $date = [
                'status' => 1,
                'msg' => '分类更新失败，请稍候再试',
            ];
        }
        return $date;
    }
}
